/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tiebreak.UI;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import tiebreak.BL.EasyException;
import tiebreak.BL.MemberWorker;

/**
 *
 * @author AngelwingedDevil
 */
public class EditMemberWindow extends JFrame
{

    private MemberForm form;
    private JLabel error;
    /**
     * 
     * @param cprseq the integer for a cpr or sequential number, the program will figure out if it's a sequentual or cpr number you want to edit from if it's longer than 4
     */
    public EditMemberWindow(int cprseq)
    { 
        try
        {
            form = new MemberForm(cprseq);
            initComponents();
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setTitle("Edit Member details for " + form.getFirstName() + " " + form.getLastName());
            setSize(getPreferredSize());
            setResizable(false);
            setVisible(true);
        }
        catch (EasyException ex)
        {
            
            dispose();
        }
    }

    private void initComponents()
    {
       
        JPanel buttonPanel = new JPanel();
        ActionListener addAl = new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                edit();
            }
        };
        ActionListener cancelAl = new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                dispose();
            }
        };
        JButton add = new JButton("Ok");
        JButton apply = new JButton("Apply");
        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(cancelAl);
        add.addActionListener(addAl);
        error = new JLabel("Current Errors: NONE");
        getContentPane().setLayout(new GridBagLayout());
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(add);
        buttonPanel.add(cancel);

        GridBagConstraints g = new GridBagConstraints();
        g.weightx = 0;
        g.weighty = 0;
        g.gridx = 0;
        g.gridy = GridBagConstraints.RELATIVE;
        g.insets = new Insets(TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE, 0, TieBreakConstraints.MEDIUMSPACE);
        g.fill = GridBagConstraints.NONE;
        getContentPane().add(form, g);
        g.fill = GridBagConstraints.BOTH;
        g.insets = new Insets(0, TieBreakConstraints.MEDIUMSPACE, 0, TieBreakConstraints.MEDIUMSPACE);
        getContentPane().add(buttonPanel, g);
        g.insets = new Insets(0, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE);
        getContentPane().add(error, g);
    }
    private void ok()
    {
        if(edit())
            dispose();
    }
    private void apply()
    {
        edit();
    }
    private void delete()
    {
        try
        {
            MemberWorker mw = new MemberWorker();
            mw.delete(form.getMember().getMemberID());
        }
        catch (EasyException ex)
        {
           new TieBreakErrorHandling(ex);
        }
    }
    private boolean edit()
    {
        if (form.isFormValid())
        {
            try
            {
                MemberWorker mw = new MemberWorker();
                mw.startUpdateMember(form.getMember().getMemberID(),form.getFirstName(), form.getLastName(), form.getAddress(), form.getZip(), form.getCpr(), form.getPhone(), form.getMail(), form.getMemberType().getID(), form.elite(), form.tennisSchool(),form.isCoach());
                JOptionPane.showMessageDialog(null, "Details for "+ form.getFirstName() + " has been edited", "edit Complete", JOptionPane.INFORMATION_MESSAGE);
                return true;
            }
            catch (EasyException e)
            {
            }

        }
        else
        {
            error.setText(form.getInValidMessage());
            setSize(getPreferredSize());
            pack();
        }
        return false;
    }
}
